from collections import defaultdict, deque
from typing import List


class Solution:
    def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
        in_degree = [0] * numCourses
        umap = defaultdict(list)

        for pre, course in prerequisites:
            umap[course].append(pre)
            in_degree[pre] += 1

        # 取入度为0的加入栈
        dq = deque([i for i in range(numCourses) if in_degree[i] == 0])
        result = []
        while dq:
            cur = dq.popleft()
            result.append(cur)
            for course in umap[cur]:
                in_degree[course] -= 1
                if in_degree[course] == 0:
                    dq.append(course)
        print(result)
        return len(result) == numCourses

print(Solution().canFinish(2, [[1,0],[0,1]]))